package com.thirtydays.frame.util.ext

import android.app.Activity
import androidx.fragment.app.Fragment
import com.blankj.utilcode.util.ThreadUtils

/**
 * @author 小垚
 * @创建时间： 2021/1/27
 * @描述：
 **/
fun Activity.wxPay(payData: String) {
    //微信APP支付
//    val createWXAPI = WXAPIFactory.createWXAPI(this, payData.appId)
//    createWXAPI.registerApp(payData.appId)
//    createWXAPI.sendReq(PayReq().apply {
//        appId = payData.appId
//        partnerId = payData.partnerId
//        prepayId = payData.prepayId
//        packageValue = payData.`package`
//        nonceStr = payData.nonceStr
//        timeStamp = payData.timestamp
//        sign = payData.sign
//    })
    //跳转小程序支付
//    val req = WXLaunchMiniProgram.Req()
//    req.userName = BaseConstant.WECHAT_ID
//    req.path = "" //小程序页面
//    req.miniprogramType = WXLaunchMiniProgram.Req.MINIPTOGRAM_TYPE_RELEASE;// 可选打开 开发版，体验版和正式版
//    createWXAPI.sendReq(req)
}

fun Fragment.wxPay(payData:String) {
    //微信APP支付
//    val createWXAPI = WXAPIFactory.createWXAPI(context, payData.appId)
//    createWXAPI.registerApp(payData.appId)
//    createWXAPI.sendReq(PayReq().apply {
//        appId = payData.appId
//        partnerId = payData.partnerId
//        prepayId = payData.prepayId
//        packageValue = payData.`package`
//        nonceStr = payData.nonceStr
//        timeStamp = payData.timestamp
//        sign = payData.sign
//    })
    //跳转小程序支付
//    val req = WXLaunchMiniProgram.Req()
//    req.userName = BaseConstant.WECHAT_ID
//    req.path = "" //小程序页面
//    req.miniprogramType = WXLaunchMiniProgram.Req.MINIPTOGRAM_TYPE_RELEASE;// 可选打开 开发版，体验版和正式版
//    createWXAPI.sendReq(req)
}

/**
 * 支付宝支付
 */
fun Activity.aliPay(payData: String, function: (String) -> Unit) {
    ThreadUtils.executeByIo(object : ThreadUtils.Task<MutableMap<String, String>>() {
        override fun doInBackground(): MutableMap<String, String> {
            return mutableMapOf()
//            return PayTask(this@AliPay).payV2(payData, true)
        }

        override fun onSuccess(result: MutableMap<String, String>?) {
            result?.let {
                var resultStatus = it["resultStatus"]
                if (resultStatus != null) {
                    if(resultStatus == "9000"){
                        function(resultStatus)
                    }
                }
            }
        }

        override fun onCancel() {
        }

        override fun onFail(t: Throwable?) {
        }
    })
}

/**
 * 支付宝支付
 */
fun Fragment.aliPay(payData: String, function: (String) -> Unit) {
    ThreadUtils.executeByIo(object : ThreadUtils.Task<MutableMap<String, String>>() {
        override fun doInBackground(): MutableMap<String, String> {
            return mutableMapOf()
//            return PayTask(activity).payV2(payData, true)
        }

        override fun onSuccess(result: MutableMap<String, String>?) {
            result?.let {
                var resultStatus = it["resultStatus"]
                if (resultStatus != null) {
                    if(resultStatus == "9000"){
                        function(resultStatus)
                    }
                }
            }
        }

        override fun onCancel() {
        }

        override fun onFail(t: Throwable?) {
        }
    })
}
